package com.wbt.bambootown.ui.view.recyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by zhang on 2016/9/2.
 */
public class ItemClickListener extends RecyclerView.SimpleOnItemTouchListener {
    public interface OnItemClickListener{
        void onItemClick(View v, int position);
        void onItemLongClick(View v, int position);
    }

    private GestureDetector gestureDetector ;

    public ItemClickListener(final RecyclerView recyclerView, final OnItemClickListener onItemClickListener) {
        gestureDetector = new GestureDetector(recyclerView.getContext(),
                new GestureDetector.SimpleOnGestureListener(){
                    @Override
                    public boolean onSingleTapUp(MotionEvent e) {
                        View clickedView = recyclerView.findChildViewUnder(e.getX(),e.getY());
                        if (clickedView != null && onItemClickListener!=null) {
                            onItemClickListener.onItemClick(clickedView,recyclerView.getChildAdapterPosition(clickedView));
                        }
                        return true;
                    }

                    @Override
                    public void onLongPress(MotionEvent e) {
                        View clickedView = recyclerView.findChildViewUnder(e.getX(),e.getY());
                        if (clickedView != null && onItemClickListener!=null) {
                            onItemClickListener.onItemLongClick(clickedView,recyclerView.getChildAdapterPosition(clickedView));
                        }
                    }
                });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        gestureDetector.onTouchEvent(e);
        return false;
    }
}
